<?php

namespace Biz\service;

use Biz\model\ShopBasicInfo;
use Biz\model\ShopCommissionRate as ShopCommissionRateModel;
use Biz\pockets\constInterface\TheParagraphStore;
use Doraemon\service\Category;

/**
 * 佣金比例
 * Class ShopCommissionRate
 * @package Biz\service
 * @deprecated
 */
class ShopCommissionRate
{
    const DESIGNER_SUFFIX = '_DS';
    const DESIGNER_TITLE = '设计师';
    public static $admAllCategoryRateList = null;

    /**
     * 设置用户默认佣金比例
     * @param $uid
     * @param $ratio
     */
    public static function setDefinedRatio($uid, $ratio)
    {
        $shopId = ShopBasicInfo::getShopId($uid);
        return ShopCommissionRateModel::addDefinedRatio($uid, $shopId, $ratio);
    }

    /**
     * 设置用户类目佣金比例
     * @param $uid
     * @param array $categoryRatioList 类目佣金数组 [['cate_id'=>1,'status'=>1,'ratio'=>15].....]
     * @param $definedRatio 默认佣金比例
     */
    public static function setRatios($uid, array $categoryRatioList, $definedRatio)
    {
        $shopId = ShopBasicInfo::getShopId($uid);
        $rateList = self::getRateList($uid);
        foreach ($categoryRatioList as $row) {
            $data = [];
            $data['uid'] = $uid;
            $data['shop_id'] = $shopId;
            $data['type'] = ShopCommissionRateModel::TYPE_CATEGORY;
            $data['category_id'] = $row['cate_id'];
            $data['status'] = $row['status'];
            $data['ratio'] = isset($row['ratio']) ? $row['ratio'] : '';
            $data['is_defined_ratio'] = 0;
            if ($row['status']) {//如果选了类目并且未填写 或则等于默认佣金 则为默认佣金比例
                if (!$row['ratio'] || ((float)$row['ratio'] == $definedRatio)) {
                    $data['is_defined_ratio'] = 1;
                    $data['ratio'] = $definedRatio;
                }
            }
            if ($rateList && isset($rateList[$row['cate_id']])) {
                $where = ['uid' => $uid, 'category_id' => $data['category_id']];
                unset($data['uid']);
                unset($data['category_id']);
                ShopCommissionRateModel::updateInfo($data, $where);
            } else {
                if ($row['status']) {
                    ShopCommissionRateModel::addRatio($data);
                }
            }
        }
    }

    /**
     * 获取用户的佣金比例
     * @param $uid
     * @return array
     */
    public static function getRateList($uid)
    {
        $list = ShopCommissionRateModel::getRatioList($uid);
        $result = [];
        foreach ($list as $row) {
            $result[$row['category_id']] = $row;
        }
        return $result;
    }

    public static function getAllCategorys()
    {
        $categoryArr = Category::getAllCategorys();
        $categoryList = [];
        foreach ($categoryArr as $fatherId => $row) {
            $categoryList[$fatherId]['id'] = $fatherId;
            $categoryList[$fatherId]['cate_name'] = $row['cate_name'];
            $categoryList[$fatherId]['deep'] = $row['deep'];
            foreach ($row['children'] as $childId => $children) {
                $categoryList[$fatherId]['children'][] = ['id' => $childId, 'cate_name' => $children['cate_name'], 'deep' => $children['deep']];
            }
        }
        return $categoryList;
    }

    /**
     * @param $uid
     * @return array
     */
    public static function getCategoryRate($uid)
    {
        $allCategory = [];
        $openCategory = [];
        $list = self::getRateList($uid);
        $allCategoryList = self::getAllCategorys();
        foreach ($allCategoryList as $fatherId => $row) {
            $allchildren = [];
            $openchildren = [];
            foreach ($row['children'] as $child) {
                $id = $child['id'];
                if (isset($list[$id]) && $list[$id]['status']) {
                    $value = [
                        'cate_id' => $id,
                        'deep' => $child['deep'],
                        'cate_name' => $child['cate_name'],
                        'status' => $list[$id]['status'],
                        'ratio' => $list[$id]['ratio'],
                        'is_defined_ratio' => $list[$id]['is_defined_ratio']
                    ];
                    $allchildren[] = $value;
                    $openchildren[] = $value;
                } else {
                    $value = [
                        'cate_id' => $id,
                        'cate_name' => $child['cate_name'],
                        'deep' => $child['deep'],
                        'status' => 0,
                        'ratio' => '',
                        'is_defined_ratio' => 0
                    ];
                    $allchildren[] = $value;
                }
            }
            if ($openchildren) {
                $openCategory[$fatherId]['status'] = 1;
                $openCategory[$fatherId]['cate_id'] = $row['id'];
                $openCategory[$fatherId]['cate_name'] = $row['cate_name'];
                $openCategory[$fatherId]['deep'] = $row['deep'];
                $openCategory[$fatherId]['children'] = $openchildren;
                $allCategory[$fatherId]['status'] = 1;
            } else {
                $allCategory[$fatherId]['status'] = 0;
            }
            if ($allchildren) {
                $allCategory[$fatherId]['cate_id'] = $row['id'];
                $allCategory[$fatherId]['cate_name'] = $row['cate_name'];
                $allCategory[$fatherId]['deep'] = $row['deep'];
                $allCategory[$fatherId]['children'] = $allchildren;
            }
        }
        return [$openCategory, $allCategory];
    }

    //获取商家支持的分类列表
    public static function getShopSupportCategoryList($uid)
    {
        return self::getCategoryRate($uid)[0];
    }

    public static function getRateByCategory($shopId, $categoryId)
    {
        return ShopCommissionRateModel::getRatioInfoByCategoryId($shopId, $categoryId);
    }

    public static function getShopRatioList($uid, $categoryIds)
    {
        $result = [];
        $list = ShopCommissionRateModel::getShopRatioList($uid, $categoryIds);
        foreach ($list as $row) {
            $result[$row['category_id']] = $row;
        }
        foreach ($categoryIds as $categoryId) {
            if (!isset($result[$categoryId])) {
                $result[$categoryId] = [];
            }
        }
        return $result;
    }

    public static function admGetDefinedRate($shopId)
    {
        $info = [];
        $result = ShopCommissionRateModel::getRatioInfo($shopId, 0);
        if ($result) {
            $info = $result;
            if ($result['ratio_ext']) {
                $ratioExt = json_decode($result['ratio_ext'], true);
                foreach ($ratioExt as $key => $value) {
                    $info[$key] = isset($value['ratio']) ? $value['ratio'] : '';
                }
            }
        }
        return $info;
    }

    /**
     * @param $uid
     * @return array
     */
    public static function admGetAllCategoryRateList($uid)
    {
        $allCategory = self::admGetAllCategoryRate($uid);
        return array_values($allCategory);
    }

    public static function admGetChildrenRateList($params)
    {
        $uid = $params[0];
        $cateId = $params[1];
        $allCategory = self::admGetAllCategoryRate($uid);
        return $allCategory[$cateId]['children'];
    }

    /**
     * @param $uid
     * @return array
     */
    public static function admGetAllCategoryRate($uid)
    {
        if (!is_null(self::$admAllCategoryRateList)) {
            return self::$admAllCategoryRateList;
        }
        $allCategory = [];
        $list = self::getRateList($uid);
        $allCategoryList = self::getAllCategorys();
        $isStore = ($list && $list[0] && $list[0]['is_store']);
        $isChannel = ($list && $list[0] && $list[0]['is_channel']);
        foreach ($allCategoryList as $fatherId => $row) {
            $allChildren = [];
            $isOpen = false;
            foreach ($row['children'] as $child) {
                $id = $child['id'];
                $extRatio = null;
                if (isset($list[$id]) && $list[$id]['status']) {
                    $value = [
                        'id' => $id,
                        'cate_id' => $id,
                        'deep' => $child['deep'],
                        'cate_name' => $child['cate_name'],
                        'status' => $list[$id]['status'],
                        'ratio' => $list[$id]['ratio'],
                        'is_defined_ratio' => $list[$id]['is_defined_ratio'],
                    ];
                    $extRatio = $list[$id]['ratio_ext'] ? json_decode($list[$id]['ratio_ext'], true) : null;
                    $isOpen = true;
                } else {
                    $value = [
                        'id' => $id,
                        'cate_id' => $id,
                        'deep' => $child['deep'],
                        'cate_name' => $child['cate_name'],
                        'status' => 0,
                        'ratio' => '',
                        'is_defined_ratio' => 0
                    ];
                }
                foreach (TheParagraphStore::STORE_LIST as $key => $name) {
                    if ($isStore) {
                        $value[$key] = $extRatio && isset($extRatio[$key]) ? $extRatio[$key]['ratio'] : '';
                    }
                    if ($isChannel) {
                        $value[$key . self::DESIGNER_SUFFIX] = $extRatio && isset($extRatio[$key . self::DESIGNER_SUFFIX]['ratio']) ? $extRatio[$key . self::DESIGNER_SUFFIX]['ratio'] : '';
                    }
                }
                $allChildren[] = $value;
            }
            if ($isOpen) {
                $allCategory[$fatherId]['status'] = 1;
            } else {
                $allCategory[$fatherId]['status'] = 0;
            }
            $allCategory[$fatherId]['cate_id'] = $row['id'];
            $allCategory[$fatherId]['cate_name'] = $row['cate_name'];
            $allCategory[$fatherId]['deep'] = $row['deep'];
            $allCategory[$fatherId]['children'] = $allChildren;
            $allCategory[$fatherId]['id'] = $row['id'];
        }
        self::$admAllCategoryRateList = $allCategory;
        return $allCategory;
    }
}